package com.leetcode.array;

import java.util.Arrays;

/**
 * Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, 
 * find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
 * @author Adiy
 *
 */
public class MissingNumber {

	public static void main(String[] args) {
		int[] nums={1};
		int result =missingNumber(nums);
		System.out.println(result);

	}

    public static int missingNumber(int[] nums) {
        int len=nums.length;
        Arrays.sort(nums);
        if(nums[0]>0){
    		return 0;
    	}
		for (int i = 0, j = i + 1; i < len && j < len; i++) {
			if (nums[i] + 1 != nums[j]) {
				return nums[j] - 1;
			} else {
				j++;
			}
		}
        return nums[len-1]+1;
    }
}
